package com.show.web.sys_emp;

import com.show.dto.sys_emp.SysEmp;
import com.show.dto.sys_menu.SysMenu;
import com.show.dto.sys_role.SysRole;
import com.show.dto.sys_role.Ztree;
import com.show.filter.LoginController;
import com.show.service.emp.CommonService;
import com.show.service.emp.EmpOrgService;
import com.show.service.emp.EmpRoleService;
import com.show.service.emp.EmpService;
import com.show.service.org.SysOrgService;
import com.show.service.role.RoleService;
import com.show.service.sys_menu.MenuService;
import com.show.util.HelperClazz;
import com.show.util.PageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * 人员管理
 *
 * @Auther : 白瑜
 * @Param :
 * @date : 2018-01-25 上午 9:42
 */
@Controller
@RequestMapping(value = "/emp")
public class EmpController {

    @Autowired
    private CommonService commonService;
    @Autowired
    private EmpService empService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private SysOrgService sysOrgService;
    @Autowired
    private EmpRoleService empRoleService;
    @Autowired
    private EmpOrgService empOrgService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private LoginController loginController;


    /**
     * 跳转到人员index页面
     *
     * @return "emp/empList"
     * baiyu 20180126
     */
    @RequestMapping("/toEmp")
    public String toEmp(HttpServletRequest request, Map<String ,Object> map) throws Exception {
        try {
            Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
            Cookie cookie = (Cookie) cookieMap.get("userName");
            String userName = cookie.getValue().replace("'","");
            List<SysMenu> list = loginController.getMenus(userName);
            String empName = loginController.getEmp(userName);
            map.put("list",list);
            map.put("empName",empName);
        } catch (Exception e) {

        }
        return "emp/emp_index";
    }

    /**
     * 加载人员List页面
     * yxd 20180201
     */
    @RequestMapping("/toEmpList")
    public String toEmpList(Map<String, Object> map, SysEmp emp, Integer page, Integer pageSize, Principal principal) {
        PageUtil<SysEmp> infos = commonService.findAllEmpByPage(emp, page, pageSize);//查询用户--角色--部门有效信息
        List<SysRole> role = roleService.findAllRole();//有效角色
        if (StringUtils.isNotBlank(emp.getLoginName())) {
            map.put("loginName", emp.getLoginName());
        }
        if (StringUtils.isNotBlank(emp.getEmpName())) {
            map.put("empName", emp.getEmpName());
        }
        if (StringUtils.isNotBlank(emp.getRoleId())) {
            map.put("roleId", emp.getRoleId());
        }
        if (StringUtils.isNotBlank(emp.getOrgName())) {
            map.put("orgName", emp.getOrgName());
        }
        if (StringUtils.isNotBlank(String.valueOf(emp.getSts()))) {
            map.put("sts", emp.getSts());
        }
        map.put("role", role);
        map.put("datas", infos);
        return "emp/emp_list";
    }

    /**
     * 人员新增
     *
     * @return yxd 20180202
     */
    @RequestMapping("/toEmpInsert")
    public String toEmpInsert(Map<String, Object> map) {
        List<Ztree> role = roleService.findParentToChirld();//有效角色
        List<Ztree> org = sysOrgService.findParentToChirld();//部门信息
        map.put("role", role);
        map.put("org", org);
        List<SysEmp> model=empService.findModel();
        map.put("model", model);
        return "emp/emp_insert";
    }


    @GetMapping("/header")
    @ApiOperation(value = "跳转RoleInsert", notes = "")
    public String header(Map<String, Object> map, Principal principal) throws Exception {
//        CgEmp cgEmp = cgEmpService.getEmp(principal.getName());
//        map.put("emp", cgEmp);
        return "header";
    }


//    @ApiOperation(value = "根据部门id查询人员", notes = "桉树型结构查询部门下人员")
//    @GetMapping("/getEmpByTree/{id}")
//    @ApiImplicitParam(name = "id", paramType = "path", value = "部门ID", dataType = "String")
//    @ResponseBody
//    public List<SysEmp> getEmpByTree(@PathVariable("id") String id) {
//        List<SysEmp> empList = empService.findEmpByOrgId(id);
//        return empList;
//    }


    @ApiOperation(value = "校验人员信息", notes = "人员编码，人员姓名，登录名，联系方式，电子邮件")
    @PostMapping("/checkEmp")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "empCode", paramType = "query", value = "人员编码", dataType = "String")
            , @ApiImplicitParam(name = "empName", paramType = "query", value = "人员姓名", dataType = "String")
            , @ApiImplicitParam(name = "loginName", paramType = "query", value = "登录名", dataType = "String")
            , @ApiImplicitParam(name = "contactNumber", paramType = "query", value = "联系方式", dataType = "String")
            , @ApiImplicitParam(name = "email", paramType = "query", value = "电子邮件", dataType = "String")

    })
    public String checkEmp(Map<String, Object> map, SysEmp emp) {
        String flag = "";
        List<SysEmp> empCode = empService.checkEmp("emp_code", emp.getEmpCode());
        List<SysEmp> empName = empService.checkEmp("emp_name", emp.getEmpName());
        List<SysEmp> loginName = empService.checkEmp("login_name", emp.getLoginName());
        List<SysEmp> contactNumber = empService.checkEmp("contact_number", emp.getContactNumber());
        List<SysEmp> email = empService.checkEmp("email", emp.getEmail());
        if (empCode.size() > 0) {
            flag = "empCode";
        } else if (empName.size() > 0) {
            flag = "empName";
        } else if (loginName.size() > 0) {
            flag = "loginName";
        } else if (contactNumber.size() > 0) {
            flag = "contactNumber";
        } else if (email.size() > 0) {
            flag = "email";
        }
        return flag;
    }

    @PostMapping("/checkEmpForUpdate")
    @ResponseBody
    public String checkEmpForUpdate(Map<String, Object> map, SysEmp emp) {
        String flag = "0";
        List<SysEmp> empCode = empService.checkEmpForUpdate("emp_code", emp.getEmpCode(), emp.getEmpId().toString());
        List<SysEmp> empName = empService.checkEmpForUpdate("emp_name", emp.getEmpName(), emp.getEmpId().toString());
        List<SysEmp> loginName = empService.checkEmpForUpdate("login_name", emp.getLoginName(), emp.getEmpId().toString());
        List<SysEmp> contactNumber = empService.checkEmpForUpdate("contact_number", emp.getContactNumber(), emp.getEmpId().toString());
        if (empCode.size() > 0) {
            return "emp_code";
        } else if (empName.size() > 0) {
            return "emp_name";
        } else if (loginName.size() > 0) {
            return "login_name";
        } else if (contactNumber.size() > 0) {
            return "contact_number";
        }
        return flag;
    }

    @ApiOperation(value = "添加人员信息", notes = "人员编码，人员姓名，登录名，联系方式，电子邮件")
    @PostMapping("/insertEmp")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "empCode", paramType = "query", value = "人员编码", dataType = "String")
            , @ApiImplicitParam(name = "empName", paramType = "query", value = "人员姓名", dataType = "String")
            , @ApiImplicitParam(name = "loginName", paramType = "query", value = "登录名", dataType = "String")
            , @ApiImplicitParam(name = "contactNumber", paramType = "query", value = "联系方式", dataType = "String")
            , @ApiImplicitParam(name = "email", paramType = "query", value = "电子邮件", dataType = "String")
            , @ApiImplicitParam(name = "gender", paramType = "query", value = "性别", dataType = "String")
            , @ApiImplicitParam(name = "loginPwd", paramType = "query", value = "密码", dataType = "String")
            , @ApiImplicitParam(name = "sts", paramType = "query", value = "使用状态", dataType = "String")
            , @ApiImplicitParam(name = "stsInfo", paramType = "query", value = "状态描述", dataType = "String")
            , @ApiImplicitParam(name = "empMode", paramType = "query", value = "用工形式", dataType = "String")
            , @ApiImplicitParam(name = "roleId", paramType = "query", value = "角色", dataType = "String")
            , @ApiImplicitParam(name = "orgId", paramType = "query", value = "部门", dataType = "String")

    })
    public SysEmp insertEmp(Map<String, Object> map, SysEmp emp) {
        return empService.insertEmp(emp);
    }


    /**
     * 查看人员ById
     *
     * @return yxd 20180206
     */
    @GetMapping("/findOne/{empId}")
    @ApiOperation(value = "findOne", notes = "显示人员")
    @ApiImplicitParams({@ApiImplicitParam(name = "empId", paramType = "query", value = "人员ID", dataType = "String")
    })
    public String findOne(Map<String, Object> map, @PathVariable("empId") int emp_id) {
        SysEmp emp = empService.findOne(emp_id);
        map.put("emp", emp);
        List<SysEmp> empRoleNames = commonService.findRoleByEmpId(emp_id);
        if (empRoleNames.size() > 0) {
            map.put("roleNames", empRoleNames.get(0).getRoleName());
        }
        List<SysEmp> empOrgNames = commonService.findOrgByEmpId(emp_id);
        if (empOrgNames.size() > 0) {
            map.put("orgNames", empOrgNames.get(0).getOrgName());
        }
        return "emp/emp_detail";
    }

    /**
     * 修改人员跳转
     *
     * @return yxd 20180206
     */
    @GetMapping("/updateEmp/{empId}")
    @ApiOperation(value = "updateEmp", notes = "显示人员")
    @ApiImplicitParams({@ApiImplicitParam(name = "empId", paramType = "query", value = "人员ID", dataType = "String")
    })
    public String updateEmp(Map<String, Object> map, @PathVariable("empId") int emp_id) {
        SysEmp emp = empService.findOne(emp_id);
        map.put("emp", emp);
        List<SysEmp> empRoleNames = commonService.findRoleByEmpId(emp_id);
        if (empRoleNames.get(0).getRoleName() != null && !"".equals(empRoleNames.get(0).getRoleName())) {
            map.put("roleNames", empRoleNames.get(0).getRoleName());
            if (empRoleNames.get(0).getRoleId().contains(",")) {
                map.put("roleIds", empRoleNames.get(0).getRoleId().replaceAll(",", "\\|"));
            } else {
                map.put("roleIds", empRoleNames.get(0).getRoleId());
            }
        }
        List<SysEmp> empOrgNames = commonService.findOrgByEmpId(emp_id);
        if (empOrgNames.get(0).getOrgName() != null && !"".equals(empOrgNames.get(0).getOrgName())) {
            map.put("orgNames", empOrgNames.get(0).getOrgName());
            if (empOrgNames.get(0).getOrgId().contains(",")) {
                map.put("orgIds", empOrgNames.get(0).getOrgId().replaceAll(",", "\\|"));
            } else {
                map.put("orgIds", empOrgNames.get(0).getOrgId());
            }
        }
        List<Ztree> role = roleService.findParentToChirld();//有效角色
        List<Ztree> org = sysOrgService.findParentToChirld();//部门信息
        map.put("role", role);
        map.put("org", org);
        List<SysEmp> model=empService.findModel();
        map.put("model", model);

        return "emp/emp_update";
    }

    @ApiOperation(value = "修改人员信息", notes = "")
    @PostMapping("/updateAndSave")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "empCode", paramType = "query", value = "人员编码", dataType = "String")
            , @ApiImplicitParam(name = "empName", paramType = "query", value = "人员姓名", dataType = "String")
            , @ApiImplicitParam(name = "loginName", paramType = "query", value = "登录名", dataType = "String")
            , @ApiImplicitParam(name = "contactNumber", paramType = "query", value = "联系方式", dataType = "String")
            , @ApiImplicitParam(name = "email", paramType = "query", value = "电子邮件", dataType = "String")
            , @ApiImplicitParam(name = "gender", paramType = "query", value = "性别", dataType = "String")
            , @ApiImplicitParam(name = "loginPwd", paramType = "query", value = "密码", dataType = "String")
            , @ApiImplicitParam(name = "sts", paramType = "query", value = "使用状态", dataType = "String")
            , @ApiImplicitParam(name = "stsInfo", paramType = "query", value = "状态描述", dataType = "String")
            , @ApiImplicitParam(name = "empMode", paramType = "query", value = "用工形式", dataType = "String")
            , @ApiImplicitParam(name = "roleId", paramType = "query", value = "角色", dataType = "String")
            , @ApiImplicitParam(name = "orgId", paramType = "query", value = "部门", dataType = "String")

    })
    public SysEmp updateAndSave(Map<String, Object> map, SysEmp emp) {
        return empService.updateAndSave(emp);
    }

    @ApiOperation(value = "删除人员信息", notes = "")
    @PostMapping("/delete")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "empId", paramType = "query", value = "人员ID", dataType = "String")
    })
    public void delete(Map<String, Object> map, SysEmp emp) {
        empService.delete(emp.getEmpId());
    }

    @ApiOperation(value = "恢复人员信息", notes = "")
    @PostMapping("/reply")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "empId", paramType = "query", value = "人员ID", dataType = "String")
    })
    public void reply(Map<String, Object> map, SysEmp emp) {
        empService.reply(emp.getEmpId());
    }


    @ApiOperation(value = "重置密码", notes = "")
    @PostMapping("/updatePwd")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "empId", paramType = "query", value = "人员ID", dataType = "String")
    })
    public void updatePwd(Map<String, Object> map, SysEmp emp) {
        empService.replyPwd(emp.getEmpId());
    }


    @ApiOperation(value = "根据社区查找网格长新增", notes = "社区ID")
    @PostMapping("/choseEmpInsert")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "gridId", paramType = "path", value = "社区ID", dataType = "String")})
    public List<SysEmp> choseEmpInsert(SysEmp emp) {
        return empService.choseEmpInsert(emp.getOrgId());
    }
    @ApiOperation(value = "根据社区查找网格长修改", notes = "社区ID")
    @PostMapping("/choseEmpUpdate")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "gridId", paramType = "path", value = "社区ID", dataType = "String"),
            @ApiImplicitParam(name = "empName", paramType = "path", value = "网格长名字", dataType = "String")
    })
    public List<SysEmp> choseEmpUpdate(SysEmp emp) {
        return empService.choseEmpUpdate(emp.getOrgId(),emp.getEmpName());
    }

}
